<template>
	<jk-dialog
		v-model="showDialog"
		:title="`${dataId ? '编辑' : '新增'}-角色`"
		width="440px"
		append-to-body
		:confirm-btn-loading="confirmBtnLoading"
		@on-visible-change="onVisibleChange"
		@on-cancel="onVisibleChange(false)"
		@on-confirm="onConfirmBtn"
	>
		<el-form ref="ruleForm" :show-message="false" :model="ruleForm" :rules="rules" label-width="80px" size="medium">
			<el-row>
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="编号" prop="code">
						<el-input v-model="ruleForm.code" placeholder="" />
					</el-form-item>
				</el-col>
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="名称" prop="name">
						<el-input v-model="ruleForm.name" placeholder="" />
					</el-form-item>
				</el-col>
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="序号" prop="sort">
						<el-input-number v-model="ruleForm.sort" controls-position="right" :min="0" />
					</el-form-item>
				</el-col>
				<el-col :span="22">
					<el-form-item class="margin-bottom-10" label="备注">
						<el-input v-model="ruleForm.remark" type="textarea" placeholder="请输入"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
	</jk-dialog>
</template>

<script>
    import { addRole, getRole, updateRole } from '../../../api/system/role';

    export default {
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            dataId: {
                type: Number,
                default: null
            }
        },
        data() {
            return {
                confirmBtnLoading: false,
                ruleForm: {
                    name: '',
                    code: '',
                    sort: '',
                    remark: ''
                },
                // 表单校验
                rules: {
                    name: [{ required: true, trigger: 'change' }],
                    code: [{ required: true, trigger: 'change' }],
                    sort: [{ required: true, trigger: 'change' }]
                },
                showDialog: false
            };
        },
        methods: {
            getParamData() {
                const params = JSON.parse(JSON.stringify(this.ruleForm));
                if (!this.dataId) delete params.id;
                return params;
            },
            onConfirmBtn() {
                this.$refs['ruleForm'].validate(valid => {
                    if (valid) {
                        const api = !this.dataId ? addRole : updateRole;
                        this.confirmBtnLoading = true;
                        api(this.getParamData()).then(res => {
                            this.confirmBtnLoading = false;
                            if (res.data.status === 200) {
                                this.$tipsBar(this, 'save');
                                this.$emit('on-confirm');
                            }
                        });
                    }
                });
            },
            onVisibleChange(e) {
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                    this.$nextTick(() => {
                        this.$refs['ruleForm'].resetFields();
                    });
                }
                this.$emit('on-visible-change', e);
            },
            getDetailRequest() {
                return getRole(this.dataId).then(res => {
                    if (res.data.status === 200) {
                        this.ruleForm = res.data.res;
                    }
                });
            },
            async getDependentData() {
                if (this.dataId) await this.getDetailRequest();
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };
</script>

<style scoped lang="scss">

</style>
